<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'

require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import {queryStageMonthRangeCO2} from "@/api/carbon-manage/data";

const animationDuration = 3000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    curYear: {
      type: Array,
      default: []
    }
  },
  data() {
    return {
      chart: null,
      rangeData1: [],
      rangeData2: [],
      rangeData3: [],
      rangeData4: [],
      years: [],

    };
  },
  watch: {
    curYear(newVal, oldVal) {
      queryStageMonthRangeCO2({begin: newVal[0], end: newVal[1]}).then(res => {
        this.rangeData = res.data;
        this.rangeData1 = res.data[0].map(item => {
          return item.carbonEmission;
        });
        this.rangeData2 = res.data[1].map(item => {
          return item.carbonEmission;
        });
        this.rangeData3 = res.data[2].map(item => {
          return item.carbonEmission;
        });
        this.rangeData4 = res.data[3].map(item => {
          return item.carbonEmission;
        });
        this.years = res.data[0].map(item => {
          return {name: item.year + '', max: 3000000};
        });
        this.initChart();
      })
    },
  },
  mounted() {
    queryStageMonthRangeCO2({begin: this.curYear[0], end: this.curYear[1]}).then(res => {
      this.rangeData = res.data;
      this.rangeData1 = res.data[0].map(item => {
        return item.carbonEmission;
      });
      this.rangeData2 = res.data[1].map(item => {
        return item.carbonEmission;
      });
      this.rangeData3 = res.data[2].map(item => {
        return item.carbonEmission;
      });
      this.rangeData4 = res.data[3].map(item => {
        return item.carbonEmission;
      });
      this.years = res.data[0].map(item => {
        return {name: item.year, max: 3000000};
      });
      this.$nextTick(() => {
        this.initChart()
      });
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')

      let option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: { // 坐标轴指示器，坐标轴触发有效
            type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        radar: {
          radius: '66%',
          center: ['50%', '42%'],
          splitNumber: 8,
          splitArea: {
            areaStyle: {
              color: 'rgba(127,95,132,.3)',
              opacity: 1,
              shadowBlur: 45,
              shadowColor: 'rgba(0,0,0,.5)',
              shadowOffsetX: 0,
              shadowOffsetY: 15
            }
          },
          indicator: this.years
        },
        legend: {
          left: 'center',
          bottom: '10',
          data: ['化石燃烧', '工业生产', '电力热力', '固碳产品']
        },
        series: [{
          type: 'radar',
          symbolSize: 0,
          areaStyle: {
            normal: {
              shadowBlur: 13,
              shadowColor: 'rgba(0,0,0,.2)',
              shadowOffsetX: 0,
              shadowOffsetY: 10,
              opacity: 1
            }
          },
          data: [
            {
              value: this.rangeData1,
              name: '化石燃烧'
            },
            {
              value: this.rangeData2,
              name: '工业生产'
            },
            {
              value: this.rangeData3,
              name: '电力热力'
            },
            {
              value: this.rangeData4,
              name: '固碳产品'
            }
          ],
          animationDuration: animationDuration
        }]
      };
      this.chart.setOption(option);
    }
  }
}
</script>
